/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.tools;

import database.DBConPool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class 鱼来鱼往 extends javax.swing.JFrame {

    public static Map<String, Integer> ConfigValuesMap = new HashMap<>();

    /**
     * Creates new form 锻造控制台
     */
    public 鱼来鱼往() {
        ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource("image2/logo.png"));
        setIconImage(icon.getImage());
        setTitle("鱼来鱼往");
        initComponents();
        GetConfigValues();
        刷新飞鱼设置();
        刷新飞鱼金币开关();
        刷新飞鱼金币开关();
        刷新飞鱼金币开关();
        刷新鱼来鱼往开关();
    }

    public static void GetConfigValues() {
        //动态数据库连接
        try (Connection con = DBConPool.getInstance().getDataSource().getConnection()) {
            PreparedStatement ps = con.prepareStatement("SELECT name, val FROM ConfigValues");
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    String name = rs.getString("name");
                    int val = rs.getInt("val");
                    ConfigValuesMap.put(name, val);
                }
            }
            ps.close();
        } catch (SQLException ex) {
            System.err.println("读取动态数据库出错：" + ex.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        删除NPC = new javax.swing.JPanel();
        jPanel76 = new javax.swing.JPanel();
        jLabel329 = new javax.swing.JLabel();
        点券倍率 = new javax.swing.JTextField();
        jLabel330 = new javax.swing.JLabel();
        金币倍率 = new javax.swing.JTextField();
        jPanel77 = new javax.swing.JPanel();
        飞鱼数量 = new javax.swing.JTextField();
        点券开关 = new javax.swing.JButton();
        金币开关 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel284 = new javax.swing.JLabel();
        jLabel285 = new javax.swing.JLabel();
        jLabel286 = new javax.swing.JLabel();
        jLabel331 = new javax.swing.JLabel();
        jLabel332 = new javax.swing.JLabel();
        jLabel333 = new javax.swing.JLabel();
        jLabel334 = new javax.swing.JLabel();
        jLabel335 = new javax.swing.JLabel();
        jLabel358 = new javax.swing.JLabel();
        jLabel363 = new javax.swing.JLabel();
        jLabel364 = new javax.swing.JLabel();
        jPanel78 = new javax.swing.JPanel();
        鱼来鱼往开关 = new javax.swing.JButton();
        jLabel365 = new javax.swing.JLabel();
        jLabel366 = new javax.swing.JLabel();

        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        删除NPC.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "鱼来鱼往", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, new java.awt.Font("幼圆", 0, 24))); // NOI18N
        删除NPC.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel76.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel76.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel329.setFont(new java.awt.Font("幼圆", 0, 18)); // NOI18N
        jLabel329.setText("点券倍率；");
        jPanel76.add(jLabel329, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 350, -1, 20));
        jPanel76.add(点券倍率, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 370, 120, 30));

        jLabel330.setFont(new java.awt.Font("幼圆", 0, 18)); // NOI18N
        jLabel330.setText("金币倍率；");
        jPanel76.add(jLabel330, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 250, -1, 20));
        jPanel76.add(金币倍率, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 270, 120, 30));

        jPanel77.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "飞鱼数量", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("幼圆", 0, 18))); // NOI18N
        jPanel77.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        jPanel77.add(飞鱼数量, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 70, 120, 30));

        jPanel76.add(jPanel77, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 60, 210, 170));

        点券开关.setText("开");
        点券开关.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                点券开关ActionPerformed(evt);
            }
        });
        jPanel76.add(点券开关, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 370, 80, 30));

        金币开关.setText("开");
        金币开关.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                金币开关ActionPerformed(evt);
            }
        });
        jPanel76.add(金币开关, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 270, 80, 30));

        jButton5.setText("修改");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel76.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 490, 100, 30));

        jLabel284.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel284.setForeground(new java.awt.Color(51, 51, 255));
        jLabel284.setText("躲避值 * 点券率");
        jPanel76.add(jLabel284, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 370, 190, 30));

        jLabel285.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel285.setText("每次玩家获取奖励后会释放鱼。");
        jPanel76.add(jLabel285, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 90, 730, 30));

        jLabel286.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel286.setText("活动开始后，玩家进入2频道水下世界，即为参加活动。");
        jPanel76.add(jLabel286, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 160, 690, 30));

        jLabel331.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel331.setText("活动每 30 秒为间隔，为参加活动的玩家增加奖励。");
        jPanel76.add(jLabel331, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 190, 690, 30));

        jLabel332.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel332.setForeground(new java.awt.Color(255, 51, 51));
        jLabel332.setText("修改左边的相关属性，来决定获得的收益。");
        jPanel76.add(jLabel332, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 490, 690, 30));

        jLabel333.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel333.setText("奖励获取公式：");
        jPanel76.add(jLabel333, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 270, 190, 30));

        jLabel334.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel334.setForeground(new java.awt.Color(51, 51, 255));
        jLabel334.setText("躲避值 * 金币率");
        jPanel76.add(jLabel334, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 310, 190, 30));

        jLabel335.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel335.setForeground(new java.awt.Color(51, 51, 255));
        jLabel335.setText("躲避值 * 经验率");
        jPanel76.add(jLabel335, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 340, 190, 30));

        jLabel358.setFont(new java.awt.Font("幼圆", 0, 18)); // NOI18N
        jLabel358.setText("玩家每 30 秒增加 1 躲避值，被怪物碰到就减少 1 点躲避值。");
        jPanel76.add(jLabel358, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 230, 690, 30));

        jLabel363.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel363.setText("离开水下世界地图，离开2频道视为放弃，重置躲避值。");
        jPanel76.add(jLabel363, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 410, 690, 30));

        jLabel364.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel364.setText("活动期间再次进入可再次加入。");
        jPanel76.add(jLabel364, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 450, 690, 30));

        jPanel78.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "鱼来鱼往", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("幼圆", 0, 18))); // NOI18N
        jPanel78.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        鱼来鱼往开关.setText("开关");
        鱼来鱼往开关.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                鱼来鱼往开关ActionPerformed(evt);
            }
        });
        jPanel78.add(鱼来鱼往开关, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 30, 80, 30));

        jLabel365.setFont(new java.awt.Font("幼圆", 0, 18)); // NOI18N
        jLabel365.setForeground(new java.awt.Color(51, 102, 255));
        jLabel365.setText("开启后，每天晚上 21:30 开始，在 2 频道水下世界举行鱼来鱼往活动，相关面板可设置。");
        jPanel78.add(jLabel365, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 30, 960, 30));

        jPanel76.add(jPanel78, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 530, 910, 70));

        jLabel366.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
        jLabel366.setText("设置飞鱼数量，来决定活动的难度。");
        jPanel76.add(jLabel366, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 50, 730, 30));

        删除NPC.add(jPanel76, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 910, 600));

        getContentPane().add(删除NPC, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 930, 640));

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
    public void 按键开关(String a, int b) {
        int 检测开关 = 鱼来鱼往.ConfigValuesMap.get(a);
        PreparedStatement ps = null;
        PreparedStatement ps1 = null;
        ResultSet rs = null;
        if (检测开关 > 0) {
            try (Connection con = DBConPool.getInstance().getDataSource().getConnection()) {
                ps = con.prepareStatement("UPDATE configvalues SET Val = ? WHERE id = ?");
                ps1 = con.prepareStatement("SELECT * FROM configvalues WHERE id = ?");
                ps1.setInt(1, b);
                rs = ps1.executeQuery();
                if (rs.next()) {
                    String sqlString1 = null;
                    String sqlString2 = null;
                    String sqlString3 = null;
                    sqlString2 = "update configvalues set Val= '0' where id= '" + b + "';";
                    PreparedStatement dropperid = con.prepareStatement(sqlString2);
                    dropperid.executeUpdate(sqlString2);
                }
            } catch (SQLException ex) {
                Logger.getLogger(鱼来鱼往.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try (Connection con = DBConPool.getInstance().getDataSource().getConnection()) {
                ps = con.prepareStatement("UPDATE configvalues SET Val = ? WHERE id = ?");
                ps1 = con.prepareStatement("SELECT * FROM configvalues WHERE id = ?");
                ps1.setInt(1, b);
                rs = ps1.executeQuery();
                if (rs.next()) {
                    String sqlString1 = null;
                    String sqlString2 = null;
                    String sqlString3 = null;
                    sqlString2 = "update configvalues set Val= '1' where id='" + b + "';";
                    PreparedStatement dropperid = con.prepareStatement(sqlString2);
                    dropperid.executeUpdate(sqlString2);
                }
            } catch (SQLException ex) {
                Logger.getLogger(鱼来鱼往.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        GetConfigValues();
    }
    private void 点券开关ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_点券开关ActionPerformed
        按键开关("飞鱼点券开关", 4003);
        刷新飞鱼点券开关();
    }//GEN-LAST:event_点券开关ActionPerformed
    private void 刷新飞鱼点券开关() {
        String 显示 = "";
        int S = ConfigValuesMap.get("飞鱼点券开关");
        if (S <= 0) {
            显示 = "开";
        } else {
            显示 = "关";
        }
        点券开关.setText(显示);
    }
    private void 金币开关ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_金币开关ActionPerformed
        按键开关("飞鱼金币开关", 4002);
        刷新飞鱼金币开关();
    }//GEN-LAST:event_金币开关ActionPerformed

    private void 刷新飞鱼金币开关() {
        String 显示 = "";
        int S = ConfigValuesMap.get("飞鱼金币开关");
        if (S <= 0) {
            显示 = "开";
        } else {
            显示 = "关";
        }
        金币开关.setText(显示);
    }
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        PreparedStatement ps = null;
        PreparedStatement ps1 = null;
        ResultSet rs = null;
        boolean result1 = 飞鱼数量.getText().matches("[0-9]+");
        boolean result2 = 金币倍率.getText().matches("[0-9]+");
        boolean result3 = 点券倍率.getText().matches("[0-9]+");
        if (result1 && result2 && result3) {
            try (Connection con = DBConPool.getInstance().getDataSource().getConnection()) {
                ps = con.prepareStatement("SELECT * FROM configvalues ");
                rs = ps.executeQuery();
                if (rs.next()) {
                    String sqlString1 = null;
                    sqlString1 = "update configvalues set Val = '" + 飞鱼数量.getText() + "' where id = 4010;";
                    PreparedStatement Val = con.prepareStatement(sqlString1);
                    Val.executeUpdate(sqlString1);

                    String sqlString2 = null;
                    sqlString2 = "update configvalues set Val = '" + 金币倍率.getText() + "' where id = 4012;";
                    PreparedStatement Val3 = con.prepareStatement(sqlString2);
                    Val3.executeUpdate(sqlString2);

                    String sqlString3 = null;
                    sqlString3 = "update configvalues set Val = '" + 点券倍率.getText() + "' where id = 4013;";
                    PreparedStatement Val4 = con.prepareStatement(sqlString3);
                    Val4.executeUpdate(sqlString3);
                    GetConfigValues();
                    刷新飞鱼设置();
                    JOptionPane.showMessageDialog(null, "修改成功，已经生效");
                }
            } catch (SQLException ex) {
                Logger.getLogger(鱼来鱼往.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            JOptionPane.showMessageDialog(null, "？？？？？？？");
        }
    }//GEN-LAST:event_jButton5ActionPerformed
    private void 刷新飞鱼设置() {
        飞鱼数量.setText("" + ConfigValuesMap.get("飞鱼数量") + "");
        金币倍率.setText("" + ConfigValuesMap.get("飞鱼金币倍率") + "");
        点券倍率.setText("" + ConfigValuesMap.get("飞鱼点券倍率") + "");
    }

    private void 刷新鱼来鱼往开关() {
        String 显示 = "";
        int S = ConfigValuesMap.get("鱼来鱼往开关");
        if (S <= 0) {
            显示 = "开启";
        } else {
            显示 = "关闭";
        }
        鱼来鱼往开关.setText(显示);
    }
    private void 鱼来鱼往开关ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_鱼来鱼往开关ActionPerformed
        按键开关("鱼来鱼往开关", 2409);
        刷新鱼来鱼往开关();
    }//GEN-LAST:event_鱼来鱼往开关ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel284;
    private javax.swing.JLabel jLabel285;
    private javax.swing.JLabel jLabel286;
    private javax.swing.JLabel jLabel329;
    private javax.swing.JLabel jLabel330;
    private javax.swing.JLabel jLabel331;
    private javax.swing.JLabel jLabel332;
    private javax.swing.JLabel jLabel333;
    private javax.swing.JLabel jLabel334;
    private javax.swing.JLabel jLabel335;
    private javax.swing.JLabel jLabel358;
    private javax.swing.JLabel jLabel363;
    private javax.swing.JLabel jLabel364;
    private javax.swing.JLabel jLabel365;
    private javax.swing.JLabel jLabel366;
    private javax.swing.JPanel jPanel76;
    private javax.swing.JPanel jPanel77;
    private javax.swing.JPanel jPanel78;
    private javax.swing.JPanel 删除NPC;
    private javax.swing.JTextField 点券倍率;
    private javax.swing.JButton 点券开关;
    private javax.swing.JTextField 金币倍率;
    private javax.swing.JButton 金币开关;
    private javax.swing.JTextField 飞鱼数量;
    private javax.swing.JButton 鱼来鱼往开关;
    // End of variables declaration//GEN-END:variables
}
